layui.use(['form', 'layer', 'jquery', 'laydate'],
    function () {
        $ = layui.jquery;
        var form = layui.form,
            layer = layui.layer;
        var laydate = layui.laydate;

        //执行一个laydate实例
        laydate.render({
            elem: '#birthday' //指定元素
            , trigger: 'click'
        });

        //监听修改提交
        form.on('submit(add)',
            function (data) {
                data.field.isShow = (data.field.isShow == '启用')?1:0;
                data.field.isRecommend = (data.field.isRecommend == '启用')?1:0;

                $.ajax({
                    url:"http://localhost:8080/back/news/add",
                    type:"post",
                    /*设置请求头*/
                    headers:{"Authorization":"Bearer "+localStorage.getItem("jwtToken")},
                    data:JSON.stringify(data.field),
                    contentType:"application/json",
                    dataType:"json",
                    success:function (res) {
                        if (res.msg == "success") {
                            //发异步，把数据提交给php
                            layer.alert("新增成功", {
                                    icon: 6
                                },
                                function () {
                                    //关闭当前frame
                                    xadmin.close();

                                    // 可以对父窗口进行刷新
                                    xadmin.father_reload();
                                });
                        } else {
                            layer.alert("新增失败");
                        }
                    },
                    error:function (jqXHR, textStatus, errorThrown) {
                        /*用户未登录*/
                        if(jqXHR.status==401){
                            window.location.href = "http://localhost:63342/spring-boot-front-page/html/login.html"
                        }

                    }
                })

                return false;

            });

    });